package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.service.MerchantService;
import com.youi.service.SysuserService;
import com.youi.entity.Merchant;
import com.youi.entity.Sysuser;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.MD5;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 系统管理员控制层
* @author Administrator
*/
@Controller
public class SysuserController  extends HttpRequestManage{

		@Autowired
		private SysuserService sysuserService;
		@Autowired
		private MerchantService merchantService;

		/**
		 * I-55
		 * 添加或编辑系统管理员
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/adminInfo/addOrEditSysuser")
		public String addOrEditSysuser(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Sysuser loginsysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			Long id=optLong("id",request);
			Sysuser sysuser=null;
			if (id!=null) {
				sysuser=sysuserService.get(id);
				if (loginsysuser.getType()==1) {
					if (!loginsysuser.getMerchantId().equals(sysuser.getMerchantId())) {
						return JsonUtil.toJson(CommonInfoUtil.NOLOGIN, "非当前登陆商家主账号无操作权限");
					}
				}					
			}else {
				sysuser=new Sysuser();
				sysuser.setDelStatus(DelStatus.NODEL);
				sysuser.setGmtCreated(new Date());
				sysuser.setOpenState(Common.TRUE);
				sysuser.setRole(2);
			}
			Integer type=optInteger("type",request);
			if (type!=null){
				if (loginsysuser.getType()==1&&type==0) {
					return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "无操作权限");
				}
				sysuser.setType(type);
			}
//			Long merchantId=optLong("merchantId", request);
//			if (merchantId!=null) {
//				Map<String, Object> prams=new HashMap<String, Object>();
//				prams.put(Common.DELSTATUS, DelStatus.NODEL);
//				prams.put(Common.ID, merchantId);
//				Merchant merchant=merchantService.getByParam(prams);
//				if (merchant==null) {
//					return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "商家不存在");
//				}
//				sysuser.setMerchantId(merchantId);
//			}
			if (loginsysuser.getMerchantId()!=null) {
				sysuser.setMerchantId(loginsysuser.getMerchantId());
			}
			String account=optString("account",request);
			if (account!=null){
				if (id==null) {
					Map<String, Object> params=new HashMap<String, Object>();
					params.put(Common.DELSTATUS, DelStatus.NODEL);
					params.put("account", account);
					Sysuser othersysuser=sysuserService.getByParam(params);
					if (othersysuser!=null) {
						return JsonUtil.toJson(CommonInfoUtil.OBJECTEXIST, "账号已存在");
					}
				}
				sysuser.setAccount(account);
			}
			String password=optString("password",request);
			if (password!=null){
				sysuser.setPassword(MD5.getPwdMd5(password));
			}
			String img=optString("img",request);
			if (img!=null){
				sysuser.setImg(img);
			}
			String name=optString("name",request);
			if (name!=null){
				sysuser.setName(name);
			}
			String pms=optString("pms", request);
			if (pms!=null&&pms.length()>0) {
				sysuser.setPms(pms);
			}
			Long roleId=optLong("roleId", request);
			if (roleId!=null) {
				sysuser.setRoleId(roleId);
			}
			String remark=optString("remark",request);
			if (remark!=null){
				sysuser.setRemark(remark);
			}
			sysuser.setGmtModify(new Date());
			sysuserService.saveOrUpdate(sysuser);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 
		 * 删除系统管理员
		*/
		@ResponseBody
		@RequestMapping("delSysuserByIds")
		public String delSysuserByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Sysuser> sysuserList= sysuserService.getByParams(params);
			if (sysuserList==null||sysuserList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Sysuser sysuser : sysuserList) {
				sysuser.setDelStatus(DelStatus.DEL);
			}
			sysuserService.saveOrUpdateAll(sysuserList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-5
		 * 查询系统管理员列表
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getSysuser")
		public String getSysuser(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			Integer openState=optInteger("openState", request);
			Long sysroleId=optLong("sysroleId", request);
			String name=optString("name", request);
			String des=optString("des",request,Common.DESC);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			if (sysuser.getMerchantId()!=null) {
				prams.put("merchantId", sysuser.getMerchantId());
				prams.put("role", 2);
			}	
			if (openState!=null) {
				prams.put("openState", openState);
			}
			if (sysroleId!=null) {
				prams.put("roleId", sysroleId);
			}
			Map<String, Object> notParams=new HashMap<String, Object>();
			notParams.put("role", 0);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (name!=null&&name.trim().length()>0) {
				searchPram=new HashMap<String, Object>();
				searchPram.put("name", name);
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Sysuser> sysuserList=sysuserService.getSysuserList(prams,notParams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (sysuserList==null||sysuserList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=sysuserService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, sysuserList);		//jsonObjectList json对象集合
			sysuserService.getSysuserMsg(sysuserList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * I-56
		 * 查询系统管理员信息详情
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getSysuserById")
		public String getSysuserById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Sysuser sysuser=sysuserService.get(id);
			if (sysuser==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			sysuserService.getSysuserMsg(sysuser);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",sysuser);//jsonObject	json对象
		}
		
		/**
		 * I-57
		 * 停用或开启系统管理员
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/adminInfo/editSysuserOpenStatus")
		public String editSysuserOpenStatus(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Sysuser loginsysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			Long id=getLong("id",request);
			Integer status=getInteger(Common.STATUS, request);
			Sysuser sysuser=sysuserService.get(id);
			if (sysuser==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			if (loginsysuser.getType()==1&&loginsysuser.getMerchantId()!=sysuser.getMerchantId()) {
				return JsonUtil.toJson(CommonInfoUtil.NOLOGIN, "非当前登陆商家主账号无操作权限");	
			}
			if (loginsysuser.getType()==1&&sysuser.getType()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "无操作权限");
			}
			sysuser.setOpenState(status);
			sysuserService.saveOrUpdate(sysuser);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",sysuser);//jsonObject	json对象
		}

		/**
		 * 查询所有系统管理员
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllSysuser")
		public String getAllSysuser(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Sysuser> sysuserList= sysuserService.getByParams(null);
			if (sysuserList==null||sysuserList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",sysuserList);//jsonObjectList	json对象集合
		}
}
